<?php
include 'config.php';
global $db;
$uid = trim($_REQUEST['uid']);
if (!$uid){
    die();
}
$sql = "SELECT a.`job`,b.`level`,c.`name` FROM t_user_record a ,t_user_data b ,t_user c WHERE a.uid = b.uid AND a.uid=c.uid AND a.uid = $uid ";
$row = $db->getRow($sql);
if (!$row){
    die();
}
$reshGold=1000;
$myJob = $row['job'];
$level = $row['level'];
$act = $_REQUEST['act'];
$name = $row['name'];

if (!$myJob){
    $headerMyJob = randJob($level);   
    saveJob($headerMyJob,$uid);
}else {
    $myJob = json_decode($myJob,true);
    if ($myJob['doing']){
        $id = $myJob['doing']['id'];
        $endtime = $myJob['doing']['endtime'];
        if (time()>$endtime and $endtime>0){#如果有工作正在进行，则不能刷新列表
            $headerMyJob = randJob($level);
            saveJob($headerMyJob,$uid);
        }else {
            if ($act=='refresh'){#如果是强制刷新列表，需要扣费
                if (refreshJob($uid,$reshGold)==true){
                    $headerMyJob = randJob($level);
                    saveJob($headerMyJob,$uid);
                }
            }else{
                $myJob['doing']['lefttime']=$myJob['doing']['endtime']-time();
                $myJob['doing']['nowtime']=time()-$myJob['doing']['starttime'];
                $headerMyJob = $myJob;#从数据库里取得的工作列表
            }
        }
    }else {
        $headerMyJob = $myJob;
        if ($act=='refresh'){#如果是强制刷新列表，需要扣费
            if (refreshJob($uid,$reshGold)==true){
                $headerMyJob = randJob($level);
                saveJob($headerMyJob,$uid);
            }
            
        }
        
    }
}


//$sql ="UPDATE t_user_record set job = ''";
//$db->query($sql);
//print_r($headerMyJob);
$headerMyJob['refresh']=$reshGold;
echo json_encode($headerMyJob);
die();
function randJob($level)
{
    global $db;
    $sql = "SELECT * FROM t_jobs WHERE randlevel = $level ";
    $all = $db->getAll($sql);
    $tmpUpLevelJobs = array();
    $tmpLowLevelJobs = array();
    foreach ($all as $k=>$v){ 
        $all[$k]['id'] = intval($all[$k]['id']);
        $all[$k]['randlevel'] = intval($all[$k]['randlevel']);
        $all[$k]['level'] = intval($all[$k]['level']);
        $all[$k]['grade'] = intval($all[$k]['grade']); 
        $all[$k]['worktime'] = intval($all[$k]['worktime']);       
        $all[$k]['power'] = intval($all[$k]['power']);           
        $all[$k]['starttime']=0;
        $all[$k]['endtime']=0;
        $all[$k]['nowtime']=0;
        $all[$k]['lefttime']=0;
        $all[$k]['status']=0;  //0未开始  1已经开始      
        $all[$k]['desc']=''.ceil($v['worktime']/60).'分钟';
        $all[$k]['desc']=$all[$k]['desc'].'|'.$v['power'].'点';
        $income = $v['income'];        
        $incomeArr = json_decode($income,true);
        $all[$k]['income'] = $incomeArr;
        foreach ($incomeArr as $kk=>$vv){
            if ($vv['gold']){
                $all[$k]['gold']=$vv['gold'];
                $all[$k]['desc'] = $all[$k]['desc'].'|'.$vv['gold'].'金币';
            }
            if ($vv['exp']){
                $all[$k]['exp']=$vv['exp'];
                $all[$k]['desc'] = $all[$k]['desc'].'|'.$vv['exp'].'经验';                
            }             
        }       
    }    
    shuffle($all);
    $returnArr = array();
    $returnArr[0]=$all[0];
    $returnArr[1]=$all[1];
    $returnArr[2]=$all[2];
    $returnArr[3]=$all[4];
    $returnArr = array_sort_by_filed($returnArr, 'id', false);
    $res = array('list'=>$returnArr);    
    return $res;
}
function saveJob($jobarr,$uid){
    global $db;
    $json = mysql_real_escape_string(json_encode($jobarr));
    $sql = "UPDATE t_user_record SET `job` = '$json' WHERE uid = $uid ";    
    return $db->query($sql);
}
function refreshJob($uid,$gold){
    global $db,$server,$port;
    $data = array (	
    'add_user_data {"uid":'.$uid.',"col":"gold","add":-1000}'
    );
    //print_r($data);
    $res = socketData($server, $port, $data);
    if ($res['res']!='success'){
        return false;
    }else{
        return true;
    }
}